package com.sunny.design.singleton.serializer;

import java.io.Serializable;

/**
 * 测试序列化单例
 *
 * @author Sunny
 * @date 2019/12/29 16:27
 */
public class SerializerSingleton implements Serializable {
    private SerializerSingleton() {
        if (instance != null) {
            throw new RuntimeException("不允许反射创建!");
        }
    }

    private static final SerializerSingleton instance = new SerializerSingleton();

    public static SerializerSingleton getInstance() {
        return instance;
    }

    /**
     * 预防反序列化时创建新对象破坏单例
     */
    public Object readResolve() {
        return instance;
    }
}
